﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class DonViTinhAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHt();
            }
        }

        public void LoadHt()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            DONVITINHRepository repository = new DONVITINHRepository(new EFRepository<DONVITINH>(), iunitfwork);
            DONVITINH objdvt = null;
            string strma = "" + Request["MADVT"];
            if (!string.IsNullOrEmpty(strma))
            {
                objdvt = repository.GetById(strma);
                txtma.Value = objdvt.MADVT;
                txttendvt.Value = objdvt.DONVITINH1;
            }
        }
        public void Capnhap()
        { 
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            DONVITINHRepository repository = new DONVITINHRepository(new EFRepository<DONVITINH>(), iunitfwork);
            DONVITINH objdvt = null;
            bool iscapnhap=true;
            string strma = "" + Request["MADVT"];
            if (!string.IsNullOrEmpty(strma))
            {
                objdvt = repository.GetById(strma);
                iscapnhap = false;
            }
            else
            {
                objdvt = new DONVITINH();
                iscapnhap = true;
            }
            if (string.IsNullOrEmpty(txtma.Value) || string.IsNullOrEmpty(txttendvt.Value))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('VUi lòng nhập đầy đủ')</script>");
                return;

            }

            objdvt.MADVT = txtma.Value;
            objdvt.DONVITINH1 = txttendvt.Value;
            try
            {
                if (iscapnhap)
                {
                    repository.Add(objdvt);

                }
                repository.Save();
            }
            catch (Exception ex)
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Lỗi rồi')</script>");
            }
        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            Capnhap();
        }

        protected void lbtnthemoi_Click(object sender, EventArgs e)
        {
            txtma.Value = "";
            txttendvt.Value = "";
            Response.Redirect("DonViTinhAdd.aspx");
        }
    }
}